package com.jec.utils.tool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class PropertiesWrap {

	private Properties properties = null;
	
	public PropertiesWrap(Properties properties) {
		super();
		this.properties = properties;
	}
	
	/**
	 * check if configurations contains a key
	 */
	public boolean contains(String key) {
		return properties.containsKey(key);
	}
	
	/**
	 * Get a string value
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public String getValue(String key, String defaultValue) {
		if(properties.containsKey(key)) {
			return properties.getProperty(key);
		} else {
			properties.setProperty(key, defaultValue);
			return defaultValue;
		}
	}
	
	/**
	 * Set a string value
	 * @param key
	 * @param value
	 */
	public void setValue(String key, String value) {
		properties.setProperty(key, value);
	}
	
	/**
	 * Get a integer value
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public int getValue(String key, int defaultValue) {
		if(properties.containsKey(key)) {
			try {
				String stringValue = properties.getProperty(key);
				return Integer.parseInt(stringValue);
			} catch (NumberFormatException e) {
				properties.setProperty(key, Integer.toString(defaultValue));
				return defaultValue;
			}
		} else {
			properties.setProperty(key, Integer.toString(defaultValue));
			return defaultValue;
		}
	}
	
	/**
	 * Set a integer value
	 * @param key
	 * @param value
	 */
	public void setValue(String key, int value) {
		properties.setProperty(key, Integer.toString(value));
	}
	
	
	/**
	 * Get a integer value
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public long getValue(String key, long defaultValue) {
		if(properties.containsKey(key)) {
			try {
				String stringValue = properties.getProperty(key);
				return Long.parseLong(stringValue);
			} catch (NumberFormatException e) {
				properties.setProperty(key, Long.toString(defaultValue));
				return defaultValue;
			}
		} else {
			properties.setProperty(key, Long.toString(defaultValue));
			return defaultValue;
		}
	}
	
	/**
	 * Set a integer value
	 * @param key
	 * @param value
	 */
	public void setValue(String key, long value) {
		properties.setProperty(key, Long.toString(value));
	}
	
	
	public boolean getValue(String key, boolean defaultValue) {
		if(properties.containsKey(key)) {
			String stringValue = properties.getProperty(key);
			return Boolean.parseBoolean(stringValue);
		} else {
			properties.setProperty(key, Boolean.toString(defaultValue));
			return defaultValue;
		}
	}
	
	public void setValue(String key, boolean value) {
		properties.setProperty(key, Boolean.toString(value));
	}
	
	
	public Date getValue(String key, Date defaultValue) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(properties.containsKey(key)) {
			String stringValue = properties.getProperty(key);
			try {
				return sdf.parse(stringValue);
			} catch (ParseException e) {
				properties.setProperty(key, sdf.format(defaultValue));
				return defaultValue;
			}
		} else {
			properties.setProperty(key, sdf.format(defaultValue));
			return defaultValue;
		}
	}
	
	public void setValue(String key, Date value) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		properties.setProperty(key, sdf.format(value));
	}

}
